﻿using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinApp.i18n;
using WinApp.Model;
using WinApp.Model.AjaxResult;
using WinApp.Until;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;

namespace WinApp.ViewModel
{
    public static class CourseListModel
    {
        public static PageDataRow ReadByMyEnrollment(SearchMyCourseSessionEntity searchMyCourseSessionEntity)
        {
            PageDataRow result = new PageDataRow();
            if (GlobalVariables.SystemSwitchModel == SwitchMode.OnLine)
            {
                result = WebApiHelper.PostWebAPI<PageDataRow>("api/Admin/Enrollment/ReadByMyEnrollment", searchMyCourseSessionEntity);
            }
            else
            {
                using (var db = new SqlLiteDbContext())
                {
                    result.Total = db.Courses.Count();
                    List<MyCourseSessionEntityOutputDto> myCourseSessionEntityOutputDtos = new List<MyCourseSessionEntityOutputDto>();

                    //foreach (var item in db.Courses.ToList())
                    //{
                    //    DateTime myDate = new DateTime(item.BeginTime.Value);
                    //    string dateString = myDate.ToString("yyyy-MM-dd HH:mm:ss");
                    //    var a = myDate.Ticks;
                    //}

                    var cbtQuery = db.Courses.AsQueryable();
                    if (searchMyCourseSessionEntity.LearningStatu.Count() > 0)
                    {
                        cbtQuery = cbtQuery.Where(x => x.LearningStatu.HasValue && searchMyCourseSessionEntity.LearningStatu.Contains(x.LearningStatu.ToString()));
                    }

                    // db.Courses
                    myCourseSessionEntityOutputDtos = cbtQuery.Skip((searchMyCourseSessionEntity.PageCondition.PageIndex - 1) * searchMyCourseSessionEntity.PageCondition.PageSize).Take(searchMyCourseSessionEntity.PageCondition.PageSize)
                    .Select(x => new MyCourseSessionEntityOutputDto
                    {
                        Id = x.Id,
                        UserId = x.UserId.HasValue ? x.UserId.Value : 0,
                        CourseSessionId = x.CourseSessionId.HasValue ? x.CourseSessionId.Value : 0,
                        LearningStatu = x.LearningStatu.HasValue ? x.LearningStatu.Value : 0,
                        SurfacePlot = x.SurfacePlot != null ? x.SurfacePlot : "",
                        CourseSessionName = x.CourseSessionName != null ? x.CourseSessionName : "",
                        CourseSessionDescription = x.CourseSessionDescription != null ? x.CourseSessionDescription : "",
                        BeginTime = x.dtBeginTime,
                        //EndTime = x.EndTime,
                        //LastLanuchDateTime = x.LastLanuchDateTime,
                        Score = x.Score.HasValue ? Convert.ToInt32(x.Score.Value) : 0,
                        IsCheckMinimumLearningTime = x.IsCheckMinimumLearningTime.HasValue && x.IsCheckMinimumLearningTime.Value == 1 ? true : false,
                        MinimumLearningTime = x.MinimumLearningTime.HasValue ? x.MinimumLearningTime.Value : 0,
                        CumulativeLearningTime = x.CumulativeLearningTime.HasValue ? x.CumulativeLearningTime.Value : 0,
                        CompletePercent = x.CompletePercent != null ? x.CompletePercent : "",
                        CourseId = x.CourseId.HasValue ? x.CourseId.Value : 0,
                        CurriculumName = x.CurriculumName != null ? x.CurriculumName : "",
                        LessonCount = x.LessonCount.HasValue ? x.LessonCount.Value : 0,

                        CreatedTime = null,
                        StandardCourseSessionId = 0,
                        IsOpen = false
                    }).ToList();
                    result.Rows = JsonConvert.SerializeObject(myCourseSessionEntityOutputDtos);
                }

            }
            return result;
        }


        public static AjaxResult CheckOpenCourseDetails(TagCourseSessionDto? tagCourseSessionDto)
        {
            AjaxResult result;
            if (GlobalVariables.SystemSwitchModel == SwitchMode.OnLine)
                result = WebApiHelper.GetWebAPI<AjaxResult>("api/Admin/LearningStatuTable/CheckAllowLanuchCourseSession?enrollmentId=" + tagCourseSessionDto.CourseEnrollmentId);
            else
                result = new AjaxResult() { Type = AjaxResultType.Success };
            return result;
        }

        public static void LanuchCourseSession(TagCourseSessionDto? tagCourseSessionDto)
        {
            if (GlobalVariables.SystemSwitchModel == SwitchMode.OnLine)
            {
                string lanuchResult = WebApiHelper.GetWebAPI("api/Admin/LearningStatuTable/LanuchCourseSession?courseId=" + tagCourseSessionDto.CourseId + "&enrollmentId=" + tagCourseSessionDto.CourseEnrollmentId);
                if (lanuchResult.IsNullOrEmpty())
                {
                    UIMessageDialog.ShowMessageDialog(Language.GetLang("page_dataValidError"), UILocalize.WarningTitle, showCancelButton: false, UIStyle.Orange, false);
                    return;
                }
                bool.TryParse(lanuchResult, out bool boolLanuch);
                if (boolLanuch != true)
                {
                    UIMessageDialog.ShowMessageDialog(Language.GetLang("page_dataValidError"), UILocalize.WarningTitle, showCancelButton: false, UIStyle.Orange, false);
                    return;
                }
            }
        }
    }

    public class TagCourseSessionDto
    {
        public int CourseId { get; set; }
        public int CourseSessionId { get; set; }

        public int CourseEnrollmentId { get; set; }

        public string StrStartTime { get; set; }
        public string StrEndTime { get; set; }
    }
}
